home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / QuickTime VR / MacOS / QuickDraw™ 3D 1.0.6F4 SDK / Development / 3DMF parser / 0.9 version / MFBINRD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-24  |  6.0 KB  |  221 lines  |  [TEXT/MPS ]

  1. /*==============================================================================
  2.  *
  3.  *    File:        MFBINRD.C
  4.  *
  5.  *    Function:    Miscellaneous functions needed during binary reads.
  6.  *
  7.  *    Author(s):    Rick Wong (RWW)
  8.  *
  9.  *    Copyright:    (c) 1995 by Apple Computer, Inc., all rights reserved.
  10.  *
  11.  *    Change History (most recent first):
  12.  *        Fabio    Changed file name to 8 characters
  13.  *        F3A_RWW    Binary TOC.
  14.  *        F2O_RWW    File created.
  15.  *==============================================================================
  16.  */
  17.  
  18. #include "MFBINRD.H"
  19.  
  20. #include <string.h>            /* memcpy */
  21.  
  22. #include "MF3D.H"
  23. #include "MFINT64.H"
  24. #include "MFERRORS.H"
  25. #include "MFMACROS.H"
  26. #include "MFMEMORY.H"
  27. #include "MFOBJECT.H"
  28.  
  29. MF3DErr
  30. MF3D_BinaryReadProc(
  31.     MF3D_FilePtr        inMetafilePtr,
  32.     MF3DSize            inAmtToRead,
  33.     char *                outBuffer)
  34. {
  35.     if ((inMetafilePtr->readBuffer.bufPos + inAmtToRead) >
  36.             inMetafilePtr->readBuffer.bufSize)
  37.     {    return kMF3DErrCantParse;
  38.     }
  39.  
  40.     memcpy(outBuffer,
  41.             &inMetafilePtr->readBuffer.buf[inMetafilePtr->readBuffer.bufPos],
  42.             inAmtToRead);
  43.     inMetafilePtr->readBuffer.bufPos += inAmtToRead;
  44.  
  45.     return kMF3DNoErr;
  46. }
  47.  
  48. /*==============================================================================
  49.  *    MF3D_PreprocessBinaryFile
  50.  *
  51.  *    Read TOC into memory.
  52.  *    First, we read the metafile header [NOTE: This is inefficient because it is
  53.  *        also the first thing we will do when the next GetAnObject is called].
  54.  *    Then, we will read in the Table of Contents objects, building the tocStuff
  55.  *        table.
  56.  *==============================================================================
  57.  */
  58. MF3DErr
  59. MF3D_PreprocessBinaryFile(
  60.     MF3D_FilePtr        inMetafilePtr)
  61. {
  62.     MF3DMetafileObjPtr            headerPtr;
  63.     MF3DTableOfContentsObjPtr    tocPtr;
  64.     MF3DBinaryFilePosition        nextTocLoc;
  65.     MF3DUns32                    numReferences;
  66.     MF3DErr                        result;
  67.  
  68.     numReferences = 0;
  69.     /* tocStuff.numReferences must be initialized before calling IntReadObj */
  70.     inMetafilePtr->tocStuff.numReferences = 0;
  71.     inMetafilePtr->tocStuff.references = MF3D_Malloc(0);
  72.     inMetafilePtr->tocStuff.refSeed = 1;
  73.     inMetafilePtr->tocStuff.typeSeed = -1;
  74.  
  75.     /* Note: MF3D_IntReadObject sets headerPtr to NULL if it fails
  76.      * (so it is okay to call dispose later)
  77.      */
  78.     result = MF3D_IntReadObject(inMetafilePtr, (MF3DVoidObjPtr *)&headerPtr);
  79.  
  80.     if (result == kMF3DNoErr)
  81.     {    if (headerPtr->objectType != kMF3DObjMetafile)
  82.             result = kMF3DErrCantParse;    /* first obj is not metafile header */
  83.     }
  84.  
  85.     MFASSERT(result != kMF3DNoErr ||
  86.             !MF3DIsTextFormat(headerPtr->tocLocation->format));
  87.     AssignInt64(nextTocLoc, headerPtr->tocLocation->location.binary);
  88.  
  89.     /* If we have a table of contents */
  90.     while (result == kMF3DNoErr && (nextTocLoc.hi != 0 || nextTocLoc.lo != 0))
  91.     {    MF3D_TOCReferencePtr    refPtr;
  92.         MF3DUns32                numNewRefs;
  93.  
  94.         tocPtr = NULL;
  95.  
  96.         result = MF3DSeekPosition(inMetafilePtr, nextTocLoc);
  97.  
  98.         if (result == kMF3DNoErr)
  99.         {    result = MF3D_IntReadObject(inMetafilePtr,
  100.                     (MF3DVoidObjPtr *)&tocPtr);
  101.         }
  102.  
  103.         if (result == kMF3DNoErr)
  104.         {    if (tocPtr->objectType != kMF3DObjTableOfContents)
  105.                 result = kMF3DErrCantParse;
  106.         }
  107.  
  108.         if (result == kMF3DNoErr)
  109.         {    MFASSERT(!MF3DIsTextFormat(tocPtr->nextTOC->format));
  110.             AssignInt64(nextTocLoc, tocPtr->nextTOC->location.binary);
  111.             numNewRefs = tocPtr->nEntries;
  112.             refPtr = MF3D_Realloc(inMetafilePtr->tocStuff.references,
  113.                     ((numReferences + numNewRefs) * sizeof(*refPtr)));
  114.             if (refPtr == NULL)
  115.             {    MF3D_Free(inMetafilePtr->tocStuff.references);
  116.                 result = kMF3DErrOutOfMemory;
  117.             }
  118.         }
  119.  
  120.         if (result == kMF3DNoErr)
  121.         {    MF3D_TOCEntryPtr    tocRefPtr;
  122.  
  123.             inMetafilePtr->tocStuff.references = refPtr;
  124.             refPtr = &inMetafilePtr->tocStuff.references[numReferences - 1];
  125.             numReferences += numNewRefs;
  126.  
  127.             if (tocPtr->refSeed > inMetafilePtr->tocStuff.refSeed)
  128.                 inMetafilePtr->tocStuff.refSeed = tocPtr->refSeed;
  129.             if (tocPtr->typeSeed < inMetafilePtr->tocStuff.typeSeed)
  130.                 inMetafilePtr->tocStuff.typeSeed = tocPtr->typeSeed;
  131.             tocRefPtr = tocPtr->tocEntries;
  132.             while (numNewRefs > 0)
  133.             {    refPtr->refID = tocRefPtr->refID;
  134.                 MFASSERT(!MF3DIsTextFormat(tocRefPtr->objLocation->format));
  135.                 AssignInt64(refPtr->ref.location,
  136.                         tocRefPtr->objLocation->location.binary);
  137.                 ++refPtr;
  138.                 ++tocRefPtr;
  139.                 --numNewRefs;
  140.             }
  141.             inMetafilePtr->tocStuff.numReferences = numReferences;
  142.         }
  143.  
  144.         MF3DDisposeObject((MF3DVoidObjPtr)tocPtr);
  145.     }
  146.  
  147.     MF3DDisposeObject((MF3DVoidObjPtr)headerPtr);
  148.  
  149.     if (result == kMF3DNoErr)
  150.     {    /* Reset file back to zero to start real reading */
  151.         MF3DBinaryFilePosition    zero;
  152.  
  153.         SetInt64ToZero(zero);
  154.         result = MF3DSeekPosition(inMetafilePtr, zero);
  155.     }
  156.  
  157.     return result;
  158. }
  159.  
  160. /*==============================================================================
  161.  *    MF3D_GetRefNameB
  162.  *
  163.  *    No reference names yet for binary files
  164.  *    @@@But maybe we will want to create them?
  165.  *==============================================================================
  166.  */
  167. MF3DCStringPtr
  168. MF3D_GetRefNameB(
  169.     MF3D_FilePtr        inMetafilePtr,
  170.     MF3DReferenceID        inRefID)
  171. {
  172.     MF3D_Unused(inMetafilePtr);
  173.     MF3D_Unused(inRefID);
  174.     return NULL;
  175. }
  176.  
  177. /*==============================================================================
  178.  *    MF3D_GetBinaryRefID
  179.  *
  180.  *    Return the refID associated with inLocation, 0 if none.
  181.  *==============================================================================
  182.  */
  183. MF3DErr
  184. MF3D_GetBinaryRefID(
  185.     MF3D_FilePtr            inMetafilePtr,
  186.     MF3DBinaryFilePosition    inLocation,
  187.     MF3DReferenceID            *outRefID)
  188. {
  189.     MF3D_TOCReferencePtr    refPtr;
  190.     MF3DUns32                refCount;
  191.  
  192.     *outRefID = 0;
  193.     refCount = inMetafilePtr->tocStuff.numReferences;
  194.     refPtr = inMetafilePtr->tocStuff.references;
  195.     while (refCount > 0)
  196.     {    if (CompareInt64(refPtr->ref.location, inLocation) == 0)
  197.         {    *outRefID = refPtr->refID;
  198.             break;            /* ### LOOP EXIT if reference found */
  199.         }
  200.         --refCount;
  201.         ++refPtr;
  202.     }
  203.  
  204.     return kMF3DNoErr;        /* for now, always no err, even if no match */
  205. }
  206.  
  207. /*==============================================================================
  208.  *    MF3D_PostprocessBinaryFile
  209.  *
  210.  *    Dispose of TOC structures
  211.  *==============================================================================
  212.  */
  213. MF3DErr
  214. MF3D_PostprocessBinaryFile(
  215.     MF3D_FilePtr        inMetafilePtr)
  216. {
  217.     MF3D_Free(inMetafilePtr->tocStuff.references);
  218.     return kMF3DNoErr;
  219. }
  220.  
  221.